package com.itheima.device.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.device.dao.DeviceIndexMapper;
import com.itheima.device.dao.DeviceInfoMapper;
import com.itheima.device.dao.DeviceMapper;
import com.itheima.device.dao.IndexMapper;
import com.itheima.device.pojo.Device;
import com.itheima.device.pojo.DeviceInfo;
import com.itheima.device.pojo.Index;
import com.itheima.device.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class DeviceServiceImpl implements DeviceService {

    @Autowired
    private DeviceMapper deviceMapper;


    @Autowired
    private DeviceInfoMapper deviceInfoMapper;

   @Autowired
   private DeviceIndexMapper deviceIndexMapper;

  @Autowired
  private IndexMapper indexMapper;

    /**
     * 查询全部列表
     * @return
     */
    @Override
    public List<Device> findAll() {
        return deviceMapper.selectAll();
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Device> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        return (Page<Device>)deviceMapper.selectAll();
    }

    /**
     * 根据设备id查询,查询设备基本信息
     * @param id
     * @return
     */
    @Override
    public Device findByDevice(Integer id) {
        return deviceMapper.selectByPrimaryKey(id);
    }



    //根据设备id删除设备与设备说明书信息
    @Override
    public DeviceInfo deleteDeviceInfoByDid(Integer did) {
          return deviceInfoMapper.deleteDeviceInfoByDid(did);

    }

    @Override
    public List<Device> findDeviceById(Integer id) {

         //查找设备
        List<Device> device = deviceMapper.findDeviceById(id);
        
        for (Device device1 : device) {
            //获取设备id
            Integer deviceId = device1.getId();
            //根据设备id查询指标id
            Integer iid = deviceIndexMapper.findIidByDeviceId(deviceId);
            //根据指标Id查询指标
            List<Index> indexList = indexMapper.findIndexById(iid);
            device1.setIndexList(indexList);
            //根据设备Id查询设备说明
            List<DeviceInfo> deviceInfoList = deviceInfoMapper.findDeviceInfoByDid(id);
            device1.setDeviceInfoList(deviceInfoList);

        }
        return device;
    }

    //根据设备Id查询设备说明
    @Override
    public List<DeviceInfo> findDeviceInfoByDid(Integer id) {
       return    deviceInfoMapper.findDeviceInfoByDid(id);

    }


    /**
     * 增添设备
     * @param device
     */
    @Override
    public void add(Device device) {
        deviceMapper.insertSelective(device);
    }



    /**
     * 修改设备基本信息
     * @param device
     */
    @Override
    public void updateDevice(Device device) {
        deviceMapper.updateByPrimaryKey(device);
    }



    /**
     * 删除设备
     * @param id
     */
    @Override
    public void delete(Integer id) {

        deviceMapper.deleteByPrimaryKey(id);
    }




}
